package com.cc.servlet;

import com.alibaba.fastjson.JSON;
import com.cc.entity.MeetingRoom;
import com.cc.service.MeetingRoomService;
import com.cc.service.impl.MeetingRoomServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author CC
 * @version 1.0
 * @description: TODO
 * @date 2025/10/23 11:27
 */
@WebServlet("/meetingRoom.do")
public class MeetingRoomServlet extends HttpServlet {
    private MeetingRoomService meetingRoomService = new MeetingRoomServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method")==null?"list":req.getParameter("method");
        switch (method){
            case "list":
               list(req, resp);
                break;
            default:
                break;
        }

    }
    // 查询所有会议室
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<MeetingRoom> meetingRooms = meetingRoomService.queryAllMeetingRoom();
        //将数据转成 json 使用fastjson
        String json = JSON.toJSONString(meetingRooms);
        //将json数据写给客户端
        resp.getWriter().write(json);
//        req.setAttribute("meetingRooms", meetingRooms);
//        req.getRequestDispatcher("/meetingRoom.jsp").forward(req, resp);
    }
}
